import os

import  requests
import pymongo


url = "https://g0.gph.netease.com/ngsocial/community/stzb/cfg/skill_extra.json?gameid=g10"


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}
response = requests.get(url,headers=headers)
print(response.status_code)
print(response.text)

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["game_db"]
collection = db["skill_extra"]

data = response.json()
collection.insert_many(data)

print("数据已存入MongoDB")


# url = "https://g0.gph.netease.com/ngsocial/community/stzb/cfg/hero_extra.json?gameid=g10"
#
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
# }
#
# response = requests.get(url,headers=headers)
# print(response.status_code)
# print(response.text)
#
# client = pymongo.MongoClient("mongodb://localhost:27017/")
# db = client["game_db"]
# collection = db["heros_extra"]
#
# data = response.json()
# collection.insert_many(data)
#
# for hero in data:
#     iconId = hero["iconId"]
#     icon_url = f"https://g0.gph.netease.com/ngsocial/community/stzb/cn/cards/cut/card_small_{iconId}.jpg?gameid=g10"
#     icon_response = requests.get(icon_url,headers=headers)
#
#     icon_collection = db["heros_icon"]
#     icon_collection.insert_one({
#         "icon":iconId,
#         "hero_name": hero.get("name"),
#         "icon_data": icon_response.content
#     })
#     print(f"已保存英雄图标：{iconId}")
#
# print("数据已存入MongoDB")
#
# url = "https://g0.gph.netease.com/ngsocial/community/stzb/cfg/gear_id.json?gameid=g10"
#
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
# }
# response = requests.get(url,headers=headers)
# print(response.status_code)
# print(response.text)

# client = pymongo.MongoClient("mongodb://localhost:27017/")
# db = client["game_db"]
# collection = db["bao_extra"]

# data = response.json()
# collection.insert_many(data)
# os.makedirs("static/bao_icon", exist_ok=True)
# for hero in data:
#     iconId = hero["id"]
#     icon_url = f"https://g0.gph.netease.com/ngsocial/community/stzb/cn/gears/gear_icon/gear_icon_{iconId}.jpg?gameid=g10"
#     icon_response = requests.get(icon_url,headers=headers)
#
#     with open(f"static/bao_icon/{iconId}.jpg","wb") as f:
#         f.write(icon_response.content)
#
# print("数据已存入MongoDB")

